Skip to content
This repository was archived by the owner on Nov 9, 2026. It is now read-only.

fix: Always close cache-lib-go Pub/Sub subscriptions [DEVOP-9863] - #33

Merged
ChristianWitts merged 1 commit into
mainfrom
christian/devop-9863-cache-lib-go-always-close-pubsub
Nov 9, 2026
Merged

ChristianWitts merged 1 commit into
mainfrom
christian/devop-9863-cache-lib-go-always-close-pubsub

Conversation

@ChristianWitts

Copy link
Copy Markdown
Contributor

Pull Request Submission Checklist

Please confirm that you have done the following before requesting reviews:

  • I have confirmed that the PR type is appropriate for the change I am making according to the Honest Pull Request and Commit Message Naming Conventions.
  • I have typed an adequate description that explains why I am making this change.
  • I have installed and run standard pre-commit hooks that lints and validates my code.

Description

Each waiter in RememberBlocking opened its own Pub/Sub subscription.
go-redis v8 dials that connection outside the pool, and
UnsubscribeAndClose returned before Close when UNSUBSCRIBE failed, for
example after a connection reset or a cancelled request. The receive
goroutine of go-redis then redials forever. In PD #20731 this kept
Memorystore at its connection limit, and fnx-api-gateway pods stayed at
about 3.5 times their usual goroutine count after the traffic dropped.

We now always close the subscription and clear it, also when
UNSUBSCRIBE fails, on a context that the request cannot cancel, with a
1 s limit. A mutex makes the cleanup run once. Unsubscribe also closes,
because it dropped the PubSub without Close. The wait uses a select
with a context timeout in place of the timeout goroutine.

We also collapse concurrent waits for the same client, key and
SubscriptionTimeout with singleflight, so one pod holds at most one
Pub/Sub wait per key and timeout. The public API does not change;
UnsubscribeAndClose in CacheOptions now has no effect, because the
cleanup always closes. singleflight adds golang.org/x/sync v0.24.0,
which needs go 1.26.0 in go.mod.

This mirrors the same change in the monorepo copy of the library, so
both copies behave the same until the cutover.

Refs: #DEVOP-9863
Signed-off-by: Christian Witts christian@honestbank.com

No experiment because this is a bug fix in a shared library.

Validation

  • cache.go, cache_subscription.go and cache_internal_test.go are byte-identical to the monorepo copy in go/libs/cache-lib-go. The existing cache_test.go passes without changes.

  • GOTOOLCHAIN=go1.26.10 go test -race -count=1 ./...: ok (11 top-level tests, including the 6 new ones in cache_internal_test.go).

  • golangci-lint run ./... (v2.15.0, repository .golangci.json): 0 issues. golangci-lint fmt --diff ./...: no diff.

  • go vet ./...: clean. go mod tidy in the root and in examples/pubsub: no diff afterwards.

  • examples/pubsub uses a replace to the root module, so its go.mod/go.sum now list golang.org/x/sync; go build ./... there passes.

  • Mutation check: with the old early return on an UNSUBSCRIBE error, TestUnsubscribeAndClose_ClosesWhenUnsubscribeFails and TestUnsubscribeAndClose_ConcurrentCalls fail.

  • go mod tidy also drops github.com/golang/mock from the root go.mod: no committed code imports it. make generate still installs mockgen@v1.6.0 itself.

  • The pre-commit hooks were not run: .pre-commit-config.yaml pins golangci-lint v1.63.4, which cannot read the v2 .golangci.json. We ran golangci-lint v2.15.0, go vet, go mod tidy and go test -race directly instead.

Each waiter in RememberBlocking opened its own Pub/Sub subscription.
go-redis v8 dials that connection outside the pool, and
UnsubscribeAndClose returned before Close when UNSUBSCRIBE failed, for
example after a connection reset or a cancelled request. The receive
goroutine of go-redis then redials forever. In PD #20731 this kept
Memorystore at its connection limit, and fnx-api-gateway pods stayed at
about 3.5 times their usual goroutine count after the traffic dropped.

We now always close the subscription and clear it, also when
UNSUBSCRIBE fails, on a context that the request cannot cancel, with a
1 s limit. A mutex makes the cleanup run once. Unsubscribe also closes,
because it dropped the PubSub without Close. The wait uses a select
with a context timeout in place of the timeout goroutine.

We also collapse concurrent waits for the same client, key and
SubscriptionTimeout with singleflight, so one pod holds at most one
Pub/Sub wait per key and timeout. The public API does not change;
UnsubscribeAndClose in CacheOptions now has no effect, because the
cleanup always closes. singleflight adds golang.org/x/sync v0.24.0,
which needs go 1.26.0 in go.mod.

This mirrors the same change in the monorepo copy of the library, so
both copies behave the same until the cutover.

Refs: #DEVOP-9863
Signed-off-by: Christian Witts <christian@honestbank.com>
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@ChristianWitts
ChristianWitts enabled auto-merge (squash) November 9, 2026 08:31
@ChristianWitts
ChristianWitts merged commit 90974cc into main Nov 9, 2026
12 of 14 checks passed
@ChristianWitts
ChristianWitts deleted the christian/devop-9863-cache-lib-go-always-close-pubsub branch November 9, 2026 08:38
@honestbank-bot

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.4.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants